﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebLayer;
using HtmlAgilityPack;
using System.Net;

namespace WebPageParser
{
    class ClassesTableEntry
    {
        public String Presence = null;
        public String Date = null;
        public String Begin = null;
        public String End = null;
        public String Type = null;
        public String Course = null;
        public String Unit = null;
    }

    class ClassesWebPage: SiteWebPage
    {
        private ClassesTableEntry m_attendanceTableHeader = null;
        public ClassesTableEntry AttendanceTableHeader
        {
            get
            {
                if (m_attendanceTableHeader == null)
                {
                    Request();
                }
                return m_attendanceTableHeader;
            }
        }

        private List<ClassesTableEntry> m_attendanceTableContent = null;
        public List<ClassesTableEntry> AttendanceTableContent
        {
            get
            {
                if (m_attendanceTableContent == null)
                {
                    Request();
                }
                return m_attendanceTableContent;
            }
        }

        private ClassesTableEntry m_cancelledTableHeader = null;
        public ClassesTableEntry CancelledTableHeader
        {
            get
            {
                if (m_cancelledTableHeader == null)
                {
                    Request();
                }
                return m_cancelledTableHeader;
            }
        }

        private List<ClassesTableEntry> m_cancelledTableContent = null;
        public List<ClassesTableEntry> CancelledTableContent
        {
            get
            {
                if (m_cancelledTableContent == null)
                {
                    Request();
                }
                return m_cancelledTableContent;
            }
        }

        public ClassesWebPage() :
            base("Classes", GetActionPageUri("classes"))
        {
        }

        protected override void OnRequest(HttpWebRequest request)
        {
            m_attendanceTableHeader = new ClassesTableEntry();
            m_attendanceTableContent = new List<ClassesTableEntry>();
            m_cancelledTableHeader = new ClassesTableEntry();
            m_cancelledTableContent = new List<ClassesTableEntry>();

            base.OnRequest(request);
        }

        protected override void OnResponse(HttpWebRequest request, HttpWebResponse response)
        {
            base.OnResponse(request, response);

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(PageContent);
            
            HtmlNode body = doc.DocumentNode;
            
            //Results html page has only one table. Select it;
            HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//table");

            if (nodes != null)
            {
                int attendanceNodeIndex = 0;
                int cancelledNodeIndex = 1;

                /* div = "Attendance" and then table that represents it */
                if (body.ChildNodes.Count >= 2 && 
                    body.ChildNodes[0].Name == "div" && body.ChildNodes[1].Name == "div")
                {
                    attendanceNodeIndex = -1;
                    cancelledNodeIndex = 0;
                }

                if (attendanceNodeIndex >= 0 && nodes.Count > attendanceNodeIndex)
                {
                    HtmlNode attandanceNode = nodes[attendanceNodeIndex];

                    /* 7 columns in a table */
                    HtmlTableParser tableParser = new HtmlTableParser(7, attandanceNode);
                    tableParser.TransformTable<ClassesTableEntry>(m_attendanceTableContent, Transformer);
                    m_attendanceTableHeader = Transformer(tableParser.Header, tableParser.HtmlHeader);
                }

                if (cancelledNodeIndex >= 0 && nodes.Count > cancelledNodeIndex)
                {
                    HtmlNode cancelledNode = nodes[cancelledNodeIndex];

                    /* 7 columns in a table */
                    HtmlTableParser tableParser = new HtmlTableParser(7, cancelledNode);
                    tableParser.TransformTable<ClassesTableEntry>(m_cancelledTableContent, Transformer);
                    m_cancelledTableHeader = Transformer(tableParser.Header, tableParser.HtmlHeader);
                }
            }
            
        }

        private ClassesTableEntry Transformer(HtmlTableRow textRow, HtmlTableRow htmlRow)
        {
            ClassesTableEntry entry = new ClassesTableEntry();
            entry.Presence  = textRow[0];
            entry.Date      = textRow[1];
            entry.Begin     = textRow[2];
            entry.End       = textRow[3];
            entry.Type      = htmlRow[4].Replace("<br>","-");
            entry.Course    = textRow[5];
            entry.Unit      = textRow[6];
            return entry;
        }
    }
}
